function showTab(tabId) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tab-content");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tab-button");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(tabId).style.display = "block";
  event.currentTarget.className += " active";
}

function goToPage() {
  const form = document.querySelector('form[action="/submit-register"]');
  const formData = new FormData(form);
  const jsonData = {};
  const requiredFields = ['name', 'gender', 'college', 'grade', 'major', 'className', 'address', 'phone', 'password'];
  for (const field of requiredFields) {
    if (!formData.get(field)) {
      alert(`请填写 ${field} 字段`);
      return;
    }
  }
  const gender = formData.get('gender');
  if (gender !== '0' && gender !== '1') {
    alert('性别选择错误，请重新选择');
    return;
  }
  formData.forEach((value, key) => {
    jsonData[key] = value;
  });
  const submitButton = document.querySelector('input[type="submit"]');
  submitButton.disabled = true;
  submitButton.value = "注册中...";
  fetch('http://192.168.244.14:8080/users/register', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(jsonData)
  })
    .then(response => {
      if (!response.ok) {
        throw new Error('网络响应错误：' + response.status);
      }
      return response.json();
    })
    .then(data => {
      console.log('后端返回的数据:', data);
      if (data && data.code === 1) {
        const userId = data.data;
        localStorage.setItem('userId', userId);
        alert('注册成功，请登录')
        window.location.href = "http://127.0.0.1:5501/sparkResume/html/Login.html";
      } else {
        alert('注册失败：' + (data && data.msg ? data.msg : '未知错误'));
      }
    })
    .catch(error => {
      console.error('注册请求出错:', error);
      alert('注册请求出错，请检查网络连接');
    })
    .finally(() => {
      submitButton.disabled = false;
      submitButton.value = "注册";
    });
}


function userLogin() {
  const form = document.querySelector('form[action="/submit-login"]');
  const formData = new FormData(form);
  console.log('formdata', formData);

  const jsonData = {};
  const requiredFields = ['phone', 'password'];
  for (const field of requiredFields) {
    if (!formData.get(field)) {
      alert(`请填写 ${field} 字段`);
      return;
    }
  }
  formData.forEach((value, key) => {
    jsonData[key] = value;
  });

  const submitButton = document.querySelector('#login input[type="submit"]');
  submitButton.disabled = true;
  submitButton.value = "登录中...";

  fetch('http://192.168.244.14:8080/users/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(jsonData)
  })
    .then(response => {
      if (!response.ok) {
        throw new Error('网络响应错误：' + response.status);
      }
      return response.json();
    })
    .then(data => {
      console.log('后端返回的数据:', data);
      if (data && data.code === 1) {
        const userId = data.data;
        localStorage.setItem('userId', userId);
        window.location.href = "http://127.0.0.1:5501/sparkResume/html/users-function.html";
      } else {
        alert('登录失败：' + (data && data.msg ? data.msg : '未知错误'));
      }
    })
    .catch(error => {
      console.error('登录请求出错:', error);
      alert('登录请求出错，请检查网络连接');
    })
    .finally(() => {
      submitButton.disabled = false;
      submitButton.value = "登录";
    });
}

function adminLogin() {
  const form = document.querySelector('form[action="/submit-manager-login"]');
  const formData = new FormData(form);
  console.log('formdata', formData);

  const jsonData = {};
  const requiredFields = ['phone', 'password'];
  for (const field of requiredFields) {
    if (!formData.get(field)) {
      alert(`请填写 ${field} 字段`);
      return;
    }
  }
  formData.forEach((value, key) => {
    jsonData[key] = value;
  });
  const submitButton = document.querySelector('#manager-login input[type="submit"]');
  submitButton.disabled = true;
  submitButton.value = "登录中...";
  fetch('http://192.168.244.14:8080/admin/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(jsonData)
  })
    .then(response => {
      if (!response.ok) {
        throw new Error('网络响应错误：' + response.status);
      }
      return response.json();
    })
    .then(data => {
      console.log('后端返回的数据:', data);
      if (data && data.code === 1) {
        const managerId = data.data;
        localStorage.setItem('managerId', managerId);
        window.location.href = "http://127.0.0.1:5501/sparkResume/html/manager-function.html";
      } else {
        alert('登录失败：' + (data && data.msg ? data.msg : '未知错误'));
      }
    })
    .catch(error => {
      console.error('登录请求出错:', error);
      alert('登录请求出错，请检查网络连接');
    })
    .finally(() => {
      submitButton.disabled = false;
      submitButton.value = "登录";
    });
}